package com.deng.performance.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.deng.common.dao.impl.OrganizationImpl;
import com.deng.common.vo.DispenseOrganizationVo;
import com.deng.util.DateTools;
import com.deng.util.MsgReader;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

public class DispensePeriodThread extends Thread {

	public String fromDt;
	public String toDt;
	private Date startDate;
	public static void main(String s[]){
		String fromDt = "2010-5-1";
		String toDt = "2011-6-1";
//		Calendar nowCa = Calendar.getInstance();
//		System.out.println(nowCa.getTime().toLocaleString()+"               "+nowCa.getTime().getYear());
//		Calendar toCa = Calendar.getInstance();
//		toCa.clear();
//		toCa.set(nowCa.getTime().getYear()+1900, 0, nowCa.getTime().getDate());
//		System.out.println(toCa.getTime().toLocaleString());
//		toCa.set(toCa.getTime().getYear()+1900, toCa.getTime().getMonth()+1, 1);
//		System.out.println(toCa.getTime().toLocaleString());
		
		Calendar fromCa = Calendar.getInstance();
		fromCa.clear();
		fromCa.set(Integer.valueOf(fromDt.split("-")[0]), Integer.valueOf(fromDt.split("-")[1])-1, 1);
		Calendar toCa = Calendar.getInstance();
		toCa.clear();
		toCa.set(Integer.valueOf(toDt.split("-")[0]), Integer.valueOf(toDt.split("-")[1])-1, 1);
		
		while(true){			
			System.out.println(".........."+fromCa.getTime().toLocaleString());
			if(fromCa.before(toCa)){
				fromCa.set(fromCa.getTime().getYear()+1900, fromCa.getTime().getMonth()+1, 1);
			}else{
				return;
			}
		}	
	}
	
	public void setFT(String from,String to){
		fromDt =  from;
		toDt = to;
		startDate = new Date();
		//month 要-1    year 要+1900
	}
	public void recalAllDispense(String from,String to){
		try{
			DispensePeriodThread t = new DispensePeriodThread();
			t.setFT(from,to);
			t.start();
		}catch(Exception ex){
			ex.printStackTrace();
		}

	}
	
	protected ResultSet queryData(Connection conn,String sql){
		System.out.println("::::queryData:::::   "+sql);
		PreparedStatement pstmt = null;
		try{
			pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		}catch(Exception ex){
			ex.printStackTrace();	
		}
		return null;
	}
	
	@Override
	public void run() {
		try {
			Calendar fromCa = Calendar.getInstance();
			fromCa.clear();
			fromCa.set(Integer.valueOf(fromDt.split("-")[0]), Integer.valueOf(fromDt.split("-")[1])-1, 1);
			Calendar toCa = Calendar.getInstance();
			toCa.clear();
			toCa.set(Integer.valueOf(toDt.split("-")[0]), Integer.valueOf(toDt.split("-")[1])-1, 1);
			
			   
        	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        	Connection conn = DriverManager.getConnection(
        			new MsgReader().getPt("CAL_BONUSE_DB"),
        			new MsgReader().getPt("CAL_BONUSE_USER_NAME"),
        			new MsgReader().getPt("CAL_BONUSE_PW"));
        
//	        String sql = "select * from t_sys_stat_organization_vs_dispense_organization";
//	        ResultSet rs = queryData(conn, sql);
	        List<DispenseOrganizationVo> l = new OrganizationImpl().getAllDispenseOrganization(conn);//new ArrayList<DispenseOrganizationVo>();

			while(true){
				System.out.println("开始时间：  "+fromCa.getTime().toLocaleString());
				for(int i =0;i<l.size();i++){
					DispenseOrganizationVo vo = l.get(i);
					vo.stat_time_str = DateTools.dtsJustDate(fromCa.getTime());
					System.out.println("计算中：    "+vo.name+"   "+vo.stat_time_str);
					new NewCalculateImpl().calDispense(vo, conn);
//					new DispenseFirstBonusImpl().recalByNewFunctionForAllOrg(conn,vo);//老的口径
				}
				if(fromCa.before(toCa)){
					fromCa.set(fromCa.getTime().getYear()+1900, fromCa.getTime().getMonth()+1, 1);
				}else{
					System.out.println("奖金计算完成！");
					conn.close();
					MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
					AsyncMessage msg = new AsyncMessage();
					msg.setDestination("pub-consumer");
					msg.setHeader("DSSubtopic", "finish-calculate-bonus");
					msg.setMessageId(UUIDUtils.createUUID());
					msg.setTimestamp(System.currentTimeMillis());
					msg.setBody(fromDt.split("-")[0]+"年"+fromDt.split("-")[1]+"月 － "+toDt.split("-")[0]+"年"+toDt.split("-")[1]+
							"月的奖金计算已完成！    ("+DateTools.dtsWithTimeS0(startDate)+"~"+DateTools.dtsWithTimeS0(new Date())+")");
					msgBroker.routeMessageToService(msg, null);
					return;
				}
			}
		}catch (Exception e) {
            e.printStackTrace();
        }
	}


}
